#include "../mqserver/mq_consumer.hpp"
#include <gtest/gtest.h>

mqserver::ConsumerManagerPtr cmp;

class ConsumerTest : public testing::Environment 
{
public:
    virtual void SetUp() override { 
        cmp = std::make_shared<mqserver::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }
    virtual void TearDown() override {
        cmp->clear();
        std::cout << "clear finish!\n";
    }
};

void cb(const std::string& tag, const proto::message::BasicProperties* bp, const std::string& body) 
{
    std::cout << tag << " consume message: " << body << "\n";
}

TEST(consumer_test, create_test)
{
    cmp->create("consumer1", "queue1", false, cb);
    cmp->create("consumer2", "queue1", false, cb);
    cmp->create("consumer3", "queue1", false, cb);

    ASSERT_EQ(cmp->exists("queue1", "consumer1"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}   

TEST(consumer_test, remove_test)
{
    cmp->remove("queue1", "consumer1");

    ASSERT_EQ(cmp->exists("queue1", "consumer1"), false);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}   

TEST(consumer_test, get_consumer_test) 
{
    auto cp = cmp->getConsumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag_, "consumer2");

    cp = cmp->getConsumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag_, "consumer3");

    cp = cmp->getConsumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag_, "consumer2");
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest());
    (void)RUN_ALL_TESTS();
    return 0;
}